package com.example.dbdesign.controller;


import com.example.dbdesign.pojo.entity.User;
import com.example.dbdesign.pojo.vo.LoginUser;
import com.example.dbdesign.pojo.vo.Res;
import com.example.dbdesign.pojo.vo.voUser;
import com.example.dbdesign.service.UserService;
import lombok.Builder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/getById")
    public Res<User> getById(@RequestParam int id) {
        return Res.success(userService.selectById(id));
    }

    @GetMapping("/id")
    public Res<User> getById() {
        LoginUser lu = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int id = lu.getUser().getId();
        return Res.success(userService.selectById(id));
    }

    @PostMapping("/login")
    public Res login(@RequestBody Map<String, String> map) {
        String uno = map.get("uno");
        String pwd = map.get("pwd");
//        int id = Integer.valueOf(map.get("role"));
        Map map2 = userService.login(uno, pwd);
        if(map2 != null) {
            return Res.success(map2);
        }else{
            return Res.fail("登录失败");
        }
    }

    @PostMapping("/logout")
    public Res logout() {
        userService.logout();
        return Res.success("登出成功");
    }


    @DeleteMapping("/del")
    @PreAuthorize("@hasAuthority('p1')")
    public Res<Boolean> del(@RequestParam int id) {
        userService.delete(id);
        return Res.success(true);
    }

    @PostMapping("/add")
    public Res add(@RequestBody User user) {
        voUser vouser = userService.register(user);
        if(vouser != null) {
            return Res.success(vouser);
        }else{
            return Res.fail("添加失败");
        }
    }

    @PostMapping("/updateById")
    public Res updateById(@RequestBody User user) {
        voUser vouser = userService.updateById(user);
        if(vouser != null) {
            return Res.success(vouser);
        }else{
            return Res.fail("更新失败");
        }
    }

    @PostMapping("/update")
    public Res update(@RequestBody User user) {
        voUser vouser =  userService.update(user);
        if(vouser != null) {
            return Res.success(vouser);
        }else{
            return Res.fail("更新失败");
        }
    }

    @GetMapping("/list")
    public Res<List<User>> list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("page", (pageNum - 1) * pageSize);
        map.put("size", pageSize);
        List<User> ans = userService.selectByPage(map);
        return Res.success(ans);
    }

    @GetMapping("/pages")
    public Res caulate(@RequestParam int size) {
        return Res.success((userService.calSum() + size - 1) / size);
    }

    @GetMapping("/getByTypeAndDate")
    public Res<List<User>> getByTypeAndDate(@RequestParam int type, @RequestParam int date) {
        return Res.success(userService.selectByTypeAndDate(type, date));
    }

    @GetMapping("/getStuForTeacher")
    public Res<List<User>> getStuForTeacher() {
        List<User> re = userService.selectForTeacher();
        if(re != null) {
            return Res.success(re);
        }else{
            return Res.fail("请求错误");
        }
    }
}
